$(function () { // 页面加载的函数


    // 1.获取当前登录的用户
    get("auth/getUser", function (resp) {

        //
        // // 页面加载的时候异步发送请求获取购物车的数量
        // var count = 0;
        // get("car/getUserCarCount",function(resp){
        //     count = resp.count;
        // });

        // 后去购物车的数量
        var count = resp.userCarCount;

        var html = "";
        if (resp.data) {
            console.log(resp.data);
            var username = resp.data.username;
            html += "<p class=\"t-con-l f-l\">" +
                "            <span> 欢迎:【" + username + "】登录</span>\n" +
                "            <a href=\"javascript:void(0)\" onclick=\"logout()\">注销</a>\n" +
                "        </p>\n";
        } else {
            html += "        <p class=\"t-con-l f-l\" th:if=\"${session.loginUser == null}\">\n" +
                "            您还没有登录，请<a onclick='toLogin()' href=\"javascript:void(0)\">登录</a>\n" +
                "        </p>";
            ;
        }

        html += "        <ul class=\"t-con-r f-r\">\n" +
            "            <li><a href=\"#\">我 (个人中心)</a></li>\n" +
            "            <li><a href=\"http://localhost/car/getUserCarList\">我的购物车<span style='color: red'>(" + count + ")</span></a></li>\n" +
            "            <li><a href=\"#\">我的订单</a></li>\n" +
            "            <li class=\"erweima\">\n" +
            "                <a href=\"#\">扫描二维码</a>\n" +
            "                <div class=\"ewm-tu\">\n" +
            "                    <a href=\"#\"><img src=\"images/erweima-tu.jpg\" /></a>\n" +
            "                </div>\n" +
            "            </li>\n" +
            "            <div style=\"clear:both;\"></div>\n" +
            "        </ul>\n" +
            "        <div style=\"clear:both;\"></div>";
        $("#shopTitle").html(html);
    });
})

function logout() {
    get("auth/logout", function (resp) {
        if (resp.code == 0) {
            location.reload();
        }
    });
}

function toLogin() {

    // 1.先记录登录之前的地址
    var returnUrl = location.href;

    // 2.跳转到登录页面
    location.href = "http://localhost/auth/loginUser.html?returnUrl=" + returnUrl;

}